<template>
  <div class="page-container">
    <div class="control-container">
      <div class="left-panel shrink-panel" :class="isWide?'':'narrow'">
        <div class="shrink-btn-panel">
          <i :class="isWide?'el-icon-arrow-left':'el-icon-arrow-right'" @click="isWide = !isWide" />
        </div>
        <OutletTree
          v-if="isWide"
          @handleNodeClick="handleNodeClick"
        />
      </div>
      <div class="right-panel overflow-x">
        <div class="search-panel">
          <el-form :inline="true" label-width="50">
            <el-form-item label="最新">
              <el-radio-group v-model="radioData" @change="changeRadio()">
                <el-radio :label="1">当天</el-radio>
                <el-radio :label="2">3天</el-radio>
                <el-radio :label="3">7天</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="时间范围">
              <el-date-picker
                v-model="time"
                type="datetimerange"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00','23:59:59']"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
            <el-form-item label="行业类别">
              <el-select v-model="query.industryCategory" placeholder="请选择">
                <el-option
                  v-for="item in industryCategorys"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="控制级别">
              <el-select v-model="query.controlLevel" placeholder="请选择" clearable>
                <el-option
                  v-for="item in controlLevels"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="排放类型">
              <el-select v-model="query.outletType" placeholder="请选择" clearable>
                <el-option
                  v-for="item in outletTypes"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="报警类型">
              <el-select v-model="query.alarmType" placeholder="请选择" clearable>
                <el-option
                  v-for="item in alarmTypes"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="处理状态">
              <el-select v-model="query.processingStatus" placeholder="请选择" clearable>
                <el-option
                  v-for="item in processingStatuss"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="是否解除">
              <el-select v-model="query.remove" placeholder="请选择" clearable>
                <el-option
                  v-for="item in removes"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="是否通知">
              <el-select v-model="query.notice" placeholder="请选择" clearable>
                <el-option
                  v-for="item in notices"
                  :value="item.value"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="是否需要处理">
              <el-select v-model="query.process" placeholder="请选择" clearable>
                <el-option
                  v-for="item in processs"
                  :key="item.code"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="报警时长持续">
              <el-input v-model="query.duration" width="8" />
            </el-form-item>
            <el-form-item label="小时以上" />
            <el-button type="primary" @click="handleSelect">查询</el-button>
            <div style="float: right;">
              <el-button v-if="roleType !== 'e036_05'" type="primary" :disabled="resetState1" @click="handleBatchConfirm">批量确认</el-button>
              <el-button v-if="roleType !== 'e036_05'" type="primary" :disabled="resetState2" @click="handleBatch">批量处理</el-button>
              <el-button type="primary" :loading="exportLoading" @click="handleExport">导出</el-button>
            </div>

          </el-form>
        </div>

        <div class="table-panel">
          <el-table
            ref="table"
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 400px)"
            :data="tableData"
            @selection-change="handleSelectionChange"
          >
            <el-table-column align="center" type="selection" width="80" />
            <el-table-column prop="alarmInformationObj.enterpriseName" label="企业名称" />
            <el-table-column prop="alarmInformationObj.outletName" label="排口名称" />
            <el-table-column prop="alarmType" label="报警类型">
              <template slot-scope="scope">
                {{ getDictLabel('e001', scope.row.alarmType) }}
              </template>
            </el-table-column>
            <el-table-column prop="alarmInformation" label="报警信息" width="200" />
            <el-table-column prop="alarmStartTime" label="报警开始时间">
              <template slot-scope="scope">
                <div>{{ parseTime(scope.row.alarmStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="alarmEndTime" label="报警结束时间">
              <template slot-scope="scope">
                <div>{{ parseTime(scope.row.alarmEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="duration" label="持续时长">
              <template v-if="scope.row.duration" slot-scope="scope">
                {{ scope.row.durationStr }} ( {{ scope.row.duration }} )
              </template>
            </el-table-column>
            <el-table-column prop="duration" label="是否通知">
              <template slot-scope="scope">
                {{ scope.row.notice === 1 ? "通知":"未通知" }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="150">
              <template slot-scope="scope">
                <el-button v-if="scope.row.processingStatus == 'e003_01' && hasBth('alarm:update') && (roleType === 'e036_03' || roleType === 'e036_01' ||roleType === 'e036_04') " type="text" size="small" @click="handleVerify(scope.row)">确认</el-button>
                <el-button v-else-if="scope.row.processingStatus == 'e003_02' && hasBth('alarm:update') && (roleType === 'e036_03' || roleType === 'e036_01' ||roleType === 'e036_04') " type="text" size="small" @click="handleVerify(scope.row)">处理</el-button>
                <el-button v-else-if="scope.row.processingStatus == 'e003_03' && hasBth('alarm:update') && (roleType === 'e036_03' || roleType === 'e036_01')" type="text" size="small" @click="handleVerify(scope.row)">重选责任人</el-button>
                <el-button v-else-if="scope.row.processingStatus == 'e003_04' && hasBth('alarm:update') && roleType === 'e036_05'" type="text" size="small" @click="handleVerify(scope.row)">评价</el-button>
                <el-button type="text" size="small" @click="handleDetails(scope.row)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <handleBatchDialog
      :dialog-visible="dialogVisible_b"
      :dialog-type="dialogType"
      :batch="batch"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
    <handleDialog
      :dialog-visible="dialogVisible_h"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
    <personDialog
      :dialog-visible="dialogVisible_p"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
    <evaluateDialog
      :dialog-visible="dialogVisible_e"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
    <detailsDialog
      :dialog-visible="dialogVisible_d"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import OutletTree from '@/components/Tree/outletTree'
import { getList, upData, batch, download } from '@/api/business/alarm'
import { parseTime } from '@/utils/index'
import { getDictLabel, downloadFile } from '@/utils'
import handleDialog from './components/handleDialog'
import personDialog from './components/personDialog'
import evaluateDialog from './components/evaluateDialog'
import handleBatchDialog from './components/handleBatchDialog'
import detailsDialog from './components/detailsDialog'
import { getDict } from '@/utils/dict'
import { getDatefmt } from '@/utils/common'
import { hasBtnPermission as hasBth, getRoleType } from '@/utils/buttons'

export default {
  name: 'Alarm',
  components: {
    Pagination, handleDialog, personDialog, evaluateDialog, handleBatchDialog, detailsDialog, OutletTree
  },
  data() {
    return {
      isWide: true,
      query: {
        total: 0,
        page: 1,
        limit: 10,
        controlLevel: '',
        industryCategory: '',
        outletType: '',
        alarmType: '',
        processingStatus: '',
        remove: '',
        notice: null,
        process: '',
        duration: null,
        projectDepartmentId: null,
        enterpriseId: null,
        outletId: null
      },
      radioData: 1,
      time: [],
      controlLevels: [],
      industryCategorys: [],
      outletTypes: [],
      alarmTypes: [],
      processingStatuss: [],
      removes: [],
      processs: [],
      notices: [
        { value: '1', name: '通知' },
        { value: '0', name: '未通知' }
      ],
      tableData: [],
      exportLoading: false,
      tableLoading: false,
      dialogVisible_h: false,
      dialogVisible_b: false,
      dialogVisible_p: false,
      dialogVisible_e: false,
      dialogVisible_d: false,
      dialogType: 'detail',
      obj: {},
      batch: [],
      resetState1: true,
      resetState2: true,
      roleType: null
    }
  },
  created() {
    this.roleType = this.getRoleType()
    this.changeRadio()
    this.initData()
    this.industryCategorys = getDict('e013')
    this.outletTypes = getDict('e012')
    this.controlLevels = getDict('e014')
    this.alarmTypes = getDict('e001')
    this.processingStatuss = getDict('e003')
    this.removes = getDict('e002')
    this.processs = getDict('e004')
  },
  methods: {
    parseTime,
    getDictLabel,
    hasBth, getRoleType,
    changeRadio() {
      this.time = []
      const date = new Date()
      this.$set(this.time, 1, getDatefmt(date, 'yyyy-MM-dd') + ' 23:59:59')
      if (this.radioData === 1) {
        this.$set(this.time, 0, getDatefmt(new Date(), 'yyyy-MM-dd') + ' 00:00:00')
      } else if (this.radioData === 2) {
        this.$set(this.time, 0, getDatefmt(new Date(new Date().setDate(date.getDate() - 3)), 'yyyy-MM-dd') + ' 00:00:00')
      } else {
        this.$set(this.time, 0, getDatefmt(new Date(new Date().setDate(date.getDate() - 7)), 'yyyy-MM-dd') + ' 00:00:00')
      }
    },
    initData() {
      this.handleSelect()
    },
    handleSelect() {
      if (this.time !== '' && this.time !== null) {
        this.query.alarmStartTime = this.time[0]
        this.query.alarmEndTime = this.time[1]
      } else {
        this.query.alarmStartTime = null
        this.query.alarmEndTime = null
      }
      this.tableLoading = true
      getList(this.query).then(res => {
        this.tableData = res.data.rows
        this.query.total = res.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 刷新
    flushPage() {
      this.handleSelect()
    },
    handleSelectionChange(val) {
      let str = ''
      let state = true
      if (val.length > 0) {
        str = val[0].processingStatus
      }
      val.forEach(item => {
        if (item.processingStatus === str) {
          state = false
        } else {
          state = true
          return
        }
      })
      if (str === 'e003_01') {
        this.resetState1 = state
      } else if (str === 'e003_02') {
        this.resetState2 = state
      } else if (str === '') {
        this.resetState1 = state
        this.resetState2 = state
      }
    },
    // 批量确认
    handleBatchConfirm() {
      const rows = this.$refs.table.selection
      const datas = []
      rows.forEach(item => {
        datas.push({ id: item.id, processingStatus: 'e003_02' })
      })
      this.toUpDatas(datas)
    },
    // 批量处理
    handleBatch() {
      this.dialogVisible_b = true
      const rows = this.$refs.table.selection
      const ids = []
      rows.forEach(item => {
        ids.push({ id: item.id, processingStatus: null, processingResults: null })
      })
      this.batch = ids
    },
    // 导出
    handleExport() {
      this.exportLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      download(query).then(result => {
        downloadFile(result, '报警信息', 'xlsx')
        this.exportLoading = false
      }).catch(() => {
        this.exportLoading = false
      })
    },
    // 点击树
    handleNodeClick(data) {
      console.log(data)
      this.handleReset()
      if (data.level === 1) {
        this.query.projectDepartmentId = data.id
      } else if (data.level === 2) {
        this.query.enterpriseId = data.id
      } else if (data.level === 3) {
        this.query.outletId = data.id
      }
      this.handleSelect()
    },
    // 点击排口数时所要重置的条件
    handleReset() {
      this.query.total = 0
      this.query.page = 1
      this.query.limit = 10
      this.query.projectDepartmentId = null
      this.query.enterpriseId = null
      this.query.outletId = null
    },
    handleVerify(data) {
      let upDatas = ''
      if (data.processingStatus === 'e003_01') {
        upDatas = { processingStatus: 'e003_02', id: data.id }
      } else if (data.processingStatus === 'e003_02') {
        this.dialogVisible_h = true
        this.obj = data
      } else if (data.processingStatus === 'e003_03') {
        this.dialogVisible_p = true
        this.obj = data
      } else if (data.processingStatus === 'e003_04') {
        this.dialogVisible_e = true
        this.obj = data
      }

      if (upDatas !== '') {
        upData(upDatas).then(res => {
          if (res.code === 200) {
            data.processingStatus = upDatas.processingStatus
          }
        })
      }
    },
    handleDetails(data) {
      this.dialogVisible_d = true
      this.obj = data
    },
    toUpDatas(datas) {
      batch(datas).then(() => {
        this.$message.success('成功')
        this.resetState1 = true
        this.resetState2 = true
        this.flushPage()
      }).catch(() => {
      })
    },
    closeDialog() {
      this.dialogVisible_b = false
      this.dialogVisible_h = false
      this.dialogVisible_p = false
      this.dialogVisible_e = false
      this.dialogVisible_d = false
    }
  }
}

</script>

<style lang="scss" scoped>
</style>
